package com.noahplat.person.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.noahplat.common.utils.NoahEnum;
import com.noahplat.common.utils.Result;
import com.noahplat.common.utils.StatusCode;
import com.noahplat.datapro.feign.DataProFeign;
import com.noahplat.person.mapper.PersonInfoMapper;
import com.noahplat.person.pojo.PersonInfo;
import com.noahplat.person.service.PersonInfoService;
import com.noahplat.person.utils.PersonHealthDao;
import com.noahplat.person.utils.RealData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: PersonInfoServiceImpl
 * @Description: 人员详细资料实现类
 * @Author 李景星
 * @Date 2021/3/8
 * @Version 1.0
 */
@Service
public class PersonInfoServiceImpl implements PersonInfoService {

    @Autowired
    private PersonInfoMapper personInfoMapper;
    @Autowired
    private DataProFeign dataProFeign;

    /**
     * 获取人员详细资料
     *
     * @param pid
     * @return
     */
    @Override
    public Result getPersonInfo(Long pid) {
        Example example = new Example(PersonInfo.class);
        example.createCriteria().andEqualTo("pid", pid);
        PersonInfo personInfo = personInfoMapper.selectOneByExample(example);
        if (ObjectUtils.isEmpty(personInfo)) {//如果为空，也需要给前端返回数据并初始化数据库
            PersonInfo newPersonInfo = new PersonInfo(pid, "", "", "", "", "", "", "", "", "", "", "", "", "");
            personInfoMapper.insert(newPersonInfo);
            return new Result(true, StatusCode.OK, "查询成功", newPersonInfo);
        }
        return new Result(true, StatusCode.OK, "查询成功", personInfo);
    }

    /**
     * 更新人员详细资料
     *
     * @param personInfo
     * @return
     */
    @Override
    public Result updatePersonInfo(PersonInfo personInfo) {
        int res = personInfoMapper.updateByPrimaryKeySelective(personInfo);
        if (res > 0) {
            return new Result(true, StatusCode.OK, "更新成功");
        }
        return new Result(false, StatusCode.ERROR, "更新失败");
    }

    /**
     * 条件查询人员资料
     *
     * @param acctid
     * @return
     */
    @Override
    public Result getPersonInfos(Integer page, Integer limit, Long acctid, String name) {
        PageHelper.startPage(page, limit);
        List<PersonInfo> list = new ArrayList<>();
        if (!StringUtils.isEmpty(name)) {
            list = personInfoMapper.getPensonInfoByName(acctid, name);
        } else {
            list = personInfoMapper.getPensonInfoByAcctid(acctid);
        }
        if (ObjectUtils.isEmpty(list)) {
            return new Result(false, StatusCode.NOTFOUNDERROR, "无人员数据");
        }
        PageInfo<PersonInfo> pageInfo = new PageInfo<>(list);
        return new Result(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /**
     * 条件查询人员健康数据
     *
     * @param page
     * @param limit
     * @param acctid
     * @param name
     * @return
     */
    public Result getHealths(Integer page, Integer limit, Long acctid, String name) {
        PageHelper.startPage(page, limit);
        List<PersonHealthDao> list = new ArrayList<>();
        if (StringUtils.isEmpty(name)) {
            list = personInfoMapper.getHealth(acctid);
        } else {
            list = personInfoMapper.getHealthByName(acctid, name);
        }
        if (ObjectUtils.isEmpty(list)) {
            return new Result(false, StatusCode.NOTFOUNDERROR, "无人员数据");
        }
        List<String> imeilist = new ArrayList<>();
        List<PersonHealthDao> reslist = new ArrayList<>();
        for (PersonHealthDao personHealthDao : list) {
            if (!StringUtils.isEmpty(personHealthDao.getImei()) && personHealthDao.getFlag().equals("Y")) {
                imeilist.add(personHealthDao.getImei());
            } else if (!StringUtils.isEmpty(personHealthDao.getImei()) && personHealthDao.getFlag().equals("N")) {
                continue;
            }
            reslist.add(personHealthDao);
        }
        PageInfo<PersonHealthDao> repageInfo = new PageInfo<>(reslist);
        if(ObjectUtils.isEmpty(imeilist)){
            return new Result(true, StatusCode.OK, "无绑定设备",repageInfo);
        }
        String[] imeis = imeilist.toArray(new String[imeilist.size()]);
        String[] types = {NoahEnum.TEMP.getCode(), NoahEnum.HEART.getCode(), NoahEnum.BLOOD.getCode(), NoahEnum.STEP.getCode(), NoahEnum.OXYGEN.getCode()};
        Result result = dataProFeign.getReal(imeis, types);
        if (!result.isFlag()) {
            return new Result(false, StatusCode.ERROR, "获取数据失败");
        }

        List<Map> list1 = (List<Map>) result.getData();
        for (Map map : list1) {
            for (PersonHealthDao personHealthDao : reslist) {
                if (StringUtils.isEmpty(personHealthDao.getImei())) {
                    continue;
                }
                if (personHealthDao.getImei().equals(map.get("imei").toString())) {
                    if(!ObjectUtils.isEmpty(map.get("blood"))){
                        personHealthDao.setBlood(map.get("blood").toString());
                    }else {
                        personHealthDao.setBlood("");
                    }
                    if(!ObjectUtils.isEmpty(map.get("heart"))){
                        personHealthDao.setHeart(map.get("heart").toString());
                    }else {
                        personHealthDao.setHeart("");
                    }
                    if(!ObjectUtils.isEmpty(map.get("temp"))){
                        personHealthDao.setTemp(map.get("temp").toString());
                    }else {
                        personHealthDao.setTemp("");
                    }
                    if(!ObjectUtils.isEmpty(map.get("step"))){
                        personHealthDao.setStep(map.get("step").toString());
                    }else {
                        personHealthDao.setStep("");
                    }
                    personHealthDao.setOxygen("");
                    break;
                }
            }
        }
        PageInfo<PersonHealthDao> pageInfo = new PageInfo<>(reslist);
        return new Result(true, StatusCode.OK, "查询成功", pageInfo);
    }
}
